package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.calculator.unit.adapter.UnitCovertAdapter;
import com.stark.calculator.unit.constant.ConvertType;
import com.stark.calculator.unit.model.UnitBean;
import com.stark.calculator.unit.model.UnitDataProvider;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.databinding.FragmentUnitConvertBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.Str2NumUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link UnitConvertFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class UnitConvertFragment extends BaseNoModelFragment<FragmentUnitConvertBinding> {

    private ConvertType mConvertType = ConvertType.LENGTH;
    private UnitBean mBaseUnitBean;
    private List<UnitBean> mAllUnitBean;
    private UnitCovertAdapter mAdapter;
    private double mInputValue = 1;

    public static UnitConvertFragment newInstance(ConvertType type) {
        UnitConvertFragment fragment = new UnitConvertFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Extra.TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int onCreate() {
        return R.layout.fragment_unit_convert;
    }

    @Override
    protected void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mConvertType = (ConvertType) bundle.getSerializable(Extra.TYPE);
            if (mConvertType == null) {
                mConvertType = ConvertType.LENGTH;
            }
        }
        //event1
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.rlEv1Container);

        //基准单位
        updateBaseUnitView();

        //EditText
        mDataBinding.etBaseUnitValue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString().trim();
                mInputValue = Str2NumUtil.parse(text);
                mAdapter.setNewInstance(getNewUnitBean(mBaseUnitBean));
            }
        });

        //各单位数据列表
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.recycleView.setLayoutManager(layoutManager);
        UnitCovertAdapter adapter = new UnitCovertAdapter();
        mAdapter = adapter;
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mBaseUnitBean = (UnitBean) adapter.getData().get(position);
                mBaseUnitBean.value = String.valueOf(1);
                updateBaseUnitView();
                mAdapter.setNewInstance(getNewUnitBean(mBaseUnitBean));
            }
        });
        adapter.setNewInstance(getNewUnitBean(mBaseUnitBean));
        mDataBinding.recycleView.setAdapter(adapter);
    }

    @Override
    protected void initData() {

    }

    /**
     * 根据所选的基准单位获取新的转换后的数据列表
     *
     * @param baseUnitBean
     * @return
     */
    private List<UnitBean> getNewUnitBean(UnitBean baseUnitBean) {
        if (mAllUnitBean == null) {
            mAllUnitBean = UnitDataProvider.getDatas(mConvertType);
        }
        List<UnitBean> newUnitBeanList = new ArrayList<>();

        for (UnitBean bean : mAllUnitBean) {
            if (bean.cnName.equals(baseUnitBean.cnName)) {
                //基准单位对象直接过滤
                continue;
            }
            double newValue = ((1 * bean.baseRateValue) / baseUnitBean.baseRateValue) * mInputValue;
            bean.value = String.valueOf(newValue);
            newUnitBeanList.add(bean);
        }
        return newUnitBeanList;
    }

    private void updateBaseUnitView() {
        if (mBaseUnitBean == null) {
            mBaseUnitBean = UnitDataProvider.getBaseUnitBean(mConvertType);
        }
        mDataBinding.tvBaseUnitName.setText(mBaseUnitBean.cnName);
        mDataBinding.etBaseUnitValue.setText(mBaseUnitBean.value);
        mDataBinding.tvBaseUnitUnit.setText(mBaseUnitBean.enName);
    }

}